﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.CommonBase.Integration.DAO.Impl;
using Demo.Services.Contracts.DTO.Infos;
using System.Data;
using System.Data.SqlClient;
using Demo.CommonBase.Logging;
using Demo.Services.Integration.DAO.Contracts.Exceptions;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using Demo.Services.Integration.DAO.IntegrationComponents.Mappings;
using Demo.Services.Integration.DAO.IntegrationComponents.Helpers;
using Demo.CommonBase.DTO.Infos;
using Demo.Services.Contracts.DTO.Filters;

namespace Demo.Services.Integration.DAO.Impl
{
    public class TecnologyDAO: DAOBase
    {
        public TecnologyResultInfo GetTecnologies(PaginationInfo pagin, TecnologyFilter filters)
        {
            TecnologyResultInfo tecnologyList = null;
            try
            {
                IList<IDbDataParameter> parameters = TecnologyDAOHelper.GetTecnologyByPage(pagin, filters);
                DataSet ds = Retrieve("Tecnology_R",parameters);
                if (ValidateDataSet(ds))
                {
                    tecnologyList = TecnologyDAOMapping.GetTecnologyList(ds);
                }

            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "TecnologyDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "TecnologyDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return tecnologyList;
        }

        public TecnologyInfo GetTecnologyById(int id)
        {
            TecnologyInfo result = null;
            try
            {
                IList<IDbDataParameter> parameters = TecnologyDAOHelper.GetTecnologyByIdParams(id);
                DataSet ds = Retrieve("Tecnology_ById", parameters);
                if (ValidateDataSet(ds))
                {
                    result = TecnologyDAOMapping.GetTecnologyById(ds);
                }

            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "TecnologyDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "TecnologyDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return result;
        }

        public void DeleteTecnology(int id)
        {

            try
            {
                IList<IDbDataParameter> parameters = TecnologyDAOHelper.GetTecnologyByIdParams(id);
                Delete("Tecnology_D", parameters);
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "TecnologyDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "TecnologyDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

        public void UpdateTecnology(TecnologyInfo tecnology)
        {
            try
            {
                IList<IDbDataParameter> parameters = TecnologyDAOHelper.GetTecnologyUpdateParams(tecnology);
                Update("Tecnology_U", parameters);

            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "TecnologyDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "TecnologyDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

        public void SaveTecnology(TecnologyInfo tecnology)
        {
            try
            {
                IList<IDbDataParameter> parameters = TecnologyDAOHelper.GetTecnologySaveParams(tecnology);
                Create("Tecnology_C", parameters);

            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "TecnologyDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "TecnologyDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

        public List<TecnologyInfo> GetTecnologysByProyect(int proyectId)
        {
            List<TecnologyInfo> tecnologyList = null;
            IList<IDbDataParameter> paramsList;
            try
            {
                Logger.Info();
                paramsList = new List<IDbDataParameter>();
                paramsList.Add(new SqlParameter("@ProyectId", proyectId));
                DataSet ds = Retrieve("Tecnology_R_ByProyect", paramsList);
                if (ValidateDataSet(ds))
                {
                    tecnologyList = TecnologyDAOMapping.GetTecnologysByProyectMapping(ds);
                }

            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "TecnologyDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "TecnologyDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return tecnologyList;
        }


        public List<TecnologyInfo> GetTecnologysByResource(int resourceId)
        {
            List<TecnologyInfo> tecnologyList = null;
            IList<IDbDataParameter> paramsList;
            try
            {
                Logger.Info();
                paramsList = new List<IDbDataParameter>();
                paramsList.Add(new SqlParameter("@ResourceId", resourceId));
                DataSet ds = Retrieve("Tecnology_R_Resource", paramsList);
                if (ValidateDataSet(ds))
                {
                    tecnologyList = TecnologyDAOMapping.GetTecnologysByResourceMapping(ds);
                }

            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "TecnologyDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "TecnologyDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return tecnologyList;
        }
        public bool CheckName(int id, string name)
        {
            bool isValid = false;
            try
            {
                IList<IDbDataParameter> parameters = TecnologyDAOHelper.GetCheckNameParams(id, name);
                DataSet ds = Retrieve("Tecnology_R_CheckName", parameters);
                isValid = !ValidateDataSet(ds);
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "ProyectDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "ProyectDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return isValid;
        }

    }
}
